package cc.telecomdigital.tdstock.view;

import cc.telecomdigital.tdstock.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Toolbar extends LinearLayout
{
	private final static int	BTN_BACKGROUND_RESOURCES_ID	= R.drawable.app_toolbar_bottom_background;

	public Toolbar(final Context context)
	{
		super(context);
	}

	public Toolbar(final Context con, AttributeSet attrs)
	{
		super(con, attrs);
		setOrientation(HORIZONTAL);
		setBackgroundColor(getResources().getColor(
				android.R.color.transparent));
		LayoutInflater inflater = (LayoutInflater) con
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.navigation, this);

		TextView _toolbutton_stock = (TextView) findViewById(R.id.currentstockinfo);
		TextView _toolbutton_monitor = (TextView) findViewById(R.id.portfolio);
		TextView _toolbutton_index = (TextView) findViewById(R.id.index);
		TextView _toolbutton_foreignExchange = (TextView) findViewById(R.id.foreign_exchange);
		TextView _toolbutton_more = (TextView) findViewById(R.id.more);

		final int imgResIds[] = { R.drawable.ic_menu_stock,
				R.drawable.icon_list_bullets, R.drawable.ic_menu_index,
				R.drawable.ic_menu_forex, R.drawable.ic_menu_formore24x24 };
		final TextView[] navigationBtns = { _toolbutton_stock,
				_toolbutton_monitor, _toolbutton_index,
				_toolbutton_foreignExchange, _toolbutton_more };

		for (int index = 0; index < imgResIds.length; index++)
		{
			Drawable bgImg = getResources().getDrawable(imgResIds[index]);
			if (bgImg == null)
			{
				continue;
			}
			bgImg.setColorFilter(Color.WHITE, Mode.MULTIPLY);
			bgImg.setBounds(0, 0, bgImg.getMinimumWidth(), bgImg
					.getMinimumHeight());

			TextView currentBtn = navigationBtns[index];
			currentBtn.setTypeface(Typeface.MONOSPACE, Typeface.NORMAL);
			if (currentBtn == null)
			{
				continue;
			}
			currentBtn.beginBatchEdit();
			currentBtn.setCompoundDrawables(null, bgImg, null, null);
			currentBtn.postInvalidate();
			currentBtn.endBatchEdit();
		}

		TypedArray a = con.obtainStyledAttributes(attrs, R.styleable.Toolbar);
		String option = a.getString(R.styleable.Toolbar_tab_id);
		String resourceId = "cc.telecomdigital.tdstock:id/" + option;
		int optionId = getResources().getIdentifier(resourceId, null, null);
		final TextView currentOption = (TextView) findViewById(optionId);

		currentOption.setBackgroundResource(BTN_BACKGROUND_RESOURCES_ID);

		Drawable highLightImg = null;
		//hard code here
		if (option.equals("index"))
		{
			highLightImg = con.getResources().getDrawable(
					R.drawable.ic_menu_index_selected);
		}
		else if (option.equals("foreign_exchange"))
		{
			highLightImg = con.getResources().getDrawable(
					R.drawable.ic_menu_forex_selected);
		}
		else if (option.equals("portfolio"))
		{
			highLightImg = con.getResources().getDrawable(
					R.drawable.icon_list_bullets_selected);
		}
		else if (option.equals("currentstockinfo"))
		{
			highLightImg = con.getResources().getDrawable(
					R.drawable.ic_menu_stock_selected);
		}
		else
		{
			highLightImg = con.getResources().getDrawable(
					R.drawable.ic_menu_formore24x24_selected);
		}
		//hard code here

		//        highLightImg.setColorFilter(0xff4fbcef, Mode.MULTIPLY);
		highLightImg.setBounds(0, 0, highLightImg.getMinimumWidth(),
				highLightImg.getMinimumHeight());
		currentOption.setCompoundDrawables(null, highLightImg, null, null);
		currentOption.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);

		currentOption.setDrawingCacheEnabled(false);
		currentOption.requestFocus(optionId);
		currentOption.setFocusable(false);
		currentOption.setClickable(false);
	}
}
